﻿using System;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Scan
{
    public partial class frmSetBm : Form
    {
        private Jxs.Jxs jxs;
        private SetCp.SetCp cp;
        private Warehouse.Warehouse wh;
        private OperationClass _operationClass;
        private delegate void updateDG1(DataTable dt);
        private delegate void updateDG2(DataTable dt);
        private delegate void updateDG3(DataTable dt);


        private void AutoSizeComboBoxItem(object sender)
        {
            Graphics g = null; Font font = null;
            try
            {
                ComboBox senderComboBox = sender as ComboBox;

                int width = senderComboBox.Width;
                g = senderComboBox.CreateGraphics();
                font = senderComboBox.Font;

    
                int newWidth;
                // 取得数据源
                DataTable dt = (DataTable)senderComboBox.DataSource;
                foreach (DataRow s in dt.Rows)  //Loop through list items and check size of each items.
                {
                    if (s != null)
                    {
                        newWidth = (int)g.MeasureString(s["name"].ToString().Trim(), font).Width;
                        if (width < newWidth)
                            width = newWidth;   //set the width of the drop down list to the width of the largest item.
                    }
                }
                this.comboBox1.Width = width;
            }
            catch
            { }
            finally
            {
                if (g != null)
                    g.Dispose();
            }

        }


        public frmSetBm(OperationClass _operationclassValue)
        {
            InitializeComponent();

            _operationClass = _operationclassValue;
        }

        private void getCpData()
        {
            cp.Url = PublicVar.CPURL;
            DataSet ds = cp.Select_Cp_Com_All(PublicVar.COMID);
            if (ds.Tables.Count > 0)
            {
                comboBox1.DataSource = ds.Tables[0];
                comboBox1.DisplayMember = "产品名称";
                comboBox1.ValueMember = "id";
                comboBox1.Enabled = true;
                AutoSizeComboBoxItem(comboBox1);
            }
        }

        private void getKfData()
        {
            wh.Url = PublicVar.WAREHOUSEURL;
            DataSet ds =wh.Select_Warehouse_ComId(PublicVar.COMID);
            if (ds.Tables.Count > 0)
            {
                comboBox2.DataSource = ds.Tables[0];
                comboBox2.DisplayMember = "库房名称";
                comboBox2.ValueMember = "id";
                comboBox2.Enabled = true;
                AutoSizeComboBoxItem(comboBox2);
            }
        }

        private void getPqData()
        {
            jxs.Url = PublicVar.JXSURL;
            DataSet ds = jxs.Select_JxsPq_ComId(PublicVar.COMID);
            if (ds.Tables.Count > 0)
            {
                comboBox3.DataSource = ds.Tables[0];
                comboBox3.DisplayMember = "片区";
                comboBox3.ValueMember = "id";
                comboBox3.Enabled = true;
                AutoSizeComboBoxItem(comboBox3);
            }
        }

        private void frmSetBm_Load(object sender, EventArgs e)
        {

            jxs = new global::Scan.Jxs.Jxs();
            cp = new global::Scan.SetCp.SetCp();
            wh = new global::Scan.Warehouse.Warehouse();

            getCpData();

            if (_operationClass == OperationClass.ruku)
            {
                this.Text = "设置入库编码"; 
                getKfData();
                comboBox2.Visible = true;
                label2.Visible = true;
                textBox3.Visible = true;
                button2.Visible = true;
            }

            if (_operationClass == OperationClass.tuihuo)
            {
                this.Text = "设置退货编码";
                getKfData();

                comboBox2.Visible = true;
                label2.Visible = true;
                textBox3.Visible = true;
                button2.Visible = true;

                textBox2.Visible = false;
                button1.Visible = false;
                label1.Visible = false;
                comboBox1.Visible = false;
            }

            if (_operationClass == OperationClass.cuku)
            {
                this.Text = "设置出库编码";
                
                comboBox4.Visible = true;
                label5.Visible = true;
                textBox5.Visible = true;
                button4.Visible = true;

                if (PublicVar.UserJxsDj <= 0)
                {
                    getPqData();
                    comboBox3.Visible = true;
                    label4.Visible = true;
                }
                else
                {
                    jxs.Url = PublicVar.JXSURL;
                    DataSet jxsds = jxs.Select_Jxs_Two_Gs(PublicVar.UserId);
                    comboBox4.DataSource = jxsds.Tables[0];
                    comboBox4.DisplayMember = "经销商名称";
                    comboBox4.ValueMember = "id";
                    comboBox4.Enabled = true;
                    AutoSizeComboBoxItem(comboBox4);
                }
            }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请选择正确的编码！");
                return;
            }

            if (_operationClass == OperationClass.ruku)
            {
                PublicVar.RuKuBm = textBox1.Text;
            }

            if (_operationClass == OperationClass.cuku)
            {
                PublicVar.CuKuBm = textBox1.Text;
            }

            if (_operationClass == OperationClass.tuihuo)
            {
                PublicVar.TuiHuoBm = textBox1.Text;
            }

            this.Close();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_operationClass == OperationClass.cuku)
            {
                if ((comboBox3.Items.Count > 0) && (comboBox4.Items.Count > 0) && (comboBox1.Items.Count > 0))
                {
                    if ((comboBox3.SelectedIndex >= 0) && (comboBox4.SelectedIndex >= 0) && (comboBox1.SelectedIndex >= 0))
                    {
                        textBox1.Text = comboBox3.SelectedValue.ToString() + "-" + comboBox4.SelectedValue.ToString() + "-" + comboBox1.SelectedValue.ToString();
                        PublicVar.cpId = comboBox1.SelectedValue.ToString();
                        PublicVar.cpMc = comboBox1.Text;
                        PublicVar.JxsPqId = comboBox3.SelectedValue.ToString();
                        PublicVar.JxsPqMc = comboBox3.Text;
                        PublicVar.JxsId = comboBox4.SelectedValue.ToString();
                        PublicVar.JxsMc = comboBox4.Text;
                    }
                }
            }

            if (_operationClass == OperationClass.ruku)
            {
                if ((comboBox2.Items.Count > 0) && (comboBox1.Items.Count > 0))
                {
                    if ((comboBox1.SelectedIndex >= 0) && (comboBox2.SelectedIndex >= 0))
                    {
                        textBox1.Text = comboBox1.SelectedValue.ToString() + "-" + comboBox2.SelectedValue.ToString();
                        PublicVar.cpId = comboBox1.SelectedValue.ToString();
                        PublicVar.cpMc = comboBox1.Text;
                        PublicVar.kfId = comboBox2.SelectedValue.ToString();
                        PublicVar.kfMc = comboBox2.Text;
                    }
                }
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((comboBox2.Items.Count > 0) && (comboBox1.Items.Count > 0))
            {

                if ((comboBox1.SelectedIndex >= 0) && (comboBox2.SelectedIndex >= 0))
                {
                    textBox1.Text = comboBox1.SelectedValue.ToString() + "-" + comboBox2.SelectedValue.ToString();
                    PublicVar.cpId = comboBox1.SelectedValue.ToString();
                    PublicVar.cpMc = comboBox1.Text;
                    PublicVar.kfId = comboBox2.SelectedValue.ToString();
                    PublicVar.kfMc = comboBox2.Text;
                }
                else
                {
                    textBox1.Text = comboBox2.SelectedValue.ToString();
                    PublicVar.kfId = comboBox2.SelectedValue.ToString();
                    PublicVar.kfMc = comboBox2.Text;
                }
            }
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox3.SelectedValue != null)
            {
                if (PublicVar.UserJxsDj <= 0)
                {
                    DataSet jxsds = jxs.Select_pq_Jxs(comboBox3.SelectedValue.ToString());
                    comboBox4.DataSource = jxsds.Tables[0];
                    comboBox4.DisplayMember = "经销商名称";
                    comboBox4.ValueMember = "id";
                    comboBox4.Enabled = true;
                    AutoSizeComboBoxItem(comboBox4);
                }
            }

            if ((comboBox3.Items.Count > 0) && (comboBox4.Items.Count > 0) && (comboBox1.Items.Count > 0))
            {
                if ((comboBox3.SelectedIndex >= 0) && (comboBox4.SelectedIndex >= 0) && (comboBox1.SelectedIndex >= 0))
                {
                    textBox1.Text = comboBox3.SelectedValue.ToString() + "-" + comboBox4.SelectedValue.ToString() + "-" + comboBox1.SelectedValue.ToString();
                    PublicVar.cpId = comboBox1.SelectedValue.ToString();
                    PublicVar.cpMc = comboBox1.Text;
                    PublicVar.JxsPqId = comboBox3.SelectedValue.ToString();
                    PublicVar.JxsPqMc = comboBox3.Text;
                    PublicVar.JxsId = comboBox4.SelectedValue.ToString();
                    PublicVar.JxsMc = comboBox4.Text;
                }
            }
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((OperationClass.cuku == _operationClass) && (PublicVar.UserJxsDj > 0))
            {
                textBox1.Text = PublicVar.UserId + "-" + comboBox4.SelectedValue.ToString();
                PublicVar.JxsId = comboBox4.SelectedValue.ToString();
                PublicVar.JxsMc = comboBox4.Text;
            }
            else
            {
                if ((comboBox3.Items.Count > 0) && (comboBox4.Items.Count > 0) && (comboBox1.Items.Count > 0))
                {
                    if ((comboBox3.SelectedIndex >= 0) && (comboBox4.SelectedIndex >= 0) && (comboBox1.SelectedIndex >= 0))
                    {
                        textBox1.Text = comboBox3.SelectedValue.ToString() + "-" + comboBox4.SelectedValue.ToString() + "-" + comboBox1.SelectedValue.ToString();
                        PublicVar.cpId = comboBox1.SelectedValue.ToString();
                        PublicVar.cpMc = comboBox1.Text;
                        PublicVar.JxsPqId = comboBox3.SelectedValue.ToString();
                        PublicVar.JxsPqMc = comboBox3.Text;
                        PublicVar.JxsId = comboBox4.SelectedValue.ToString();
                        PublicVar.JxsMc = comboBox4.Text;
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox2.Text))
            {
                DataSet ds = new DataSet();
                ds = cp.Select_Cp_Com_WhereQuery(PublicVar.COMID, textBox2.Text);
                if ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0))
                {
                    comboBox1.DataSource = ds.Tables[0];
                    comboBox1.DisplayMember = "产品名称";
                    comboBox1.ValueMember = "id";
                    comboBox1.Enabled = true;
                    AutoSizeComboBoxItem(comboBox1);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox3.Text))
            {
                DataSet ds = new DataSet();
                ds = wh.Select_Warehouse_comId_WhereQuery(PublicVar.COMID, textBox3.Text);
                if ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0))
                {
                    comboBox2.DataSource = ds.Tables[0];
                    comboBox2.DisplayMember = "库房名称";
                    comboBox2.ValueMember = "id";
                    comboBox2.Enabled = true;
                    AutoSizeComboBoxItem(comboBox2);
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox5.Text))
            {
                DataSet ds = new DataSet();
                ds = jxs.Select_Jxs_Com_WhereQuery(PublicVar.COMID, textBox5.Text);
                if ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0))
                {
                    comboBox4.DataSource = ds.Tables[0];
                    comboBox4.DisplayMember = "经销商名称";
                    comboBox4.ValueMember = "id";
                    comboBox4.Enabled = true;
                    AutoSizeComboBoxItem(comboBox4);
                }
            }
        }
    }
}